package study.springcloud.provider.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.eggs.result.Result;
import study.eggs.result.Results;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class TimeoutController {

    @RequestMapping("/timeout")
    public Result timeout(Long timeout) {
        Stopwatch stopwatch = Stopwatch.createStarted();
        ThreadUtil.sleep(timeout, TimeUnit.SECONDS);
        log.info(">>>>>> timeout cost [{}] ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
        Map<String, Object> data = Maps.newHashMap();
        data.put("timeout", timeout);
        return Results.ok(data);
    }
}
